package com.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;

import com.model.Clazz;
import com.model.Paper;
import com.model.Proanswer;
import com.model.Programming;
import com.model.User;
import com.service.PaperService;
import com.service.TeacherService;
import com.service.UserService;
import com.util.BaseAction;
import com.util.Pagination;

public class TeacherAction extends BaseAction {

    public int getProgramId() {
        return programId;
    }

    public void setProgramId(int programId) {
        this.programId = programId;
    }

    private static final long serialVersionUID = 6096532659952115605L;

    private Integer currentpage; // 分页参数
    private Integer pagesize;

    private String add;

    private Clazz clazz;

    private String id;

    private Paper paper;

    private String paperid;

    private Integer paperId;

    private String papername;

    private Pagination paperPage;

    private PaperService paperService;

    private Proanswer proanswer;

    private List<Proanswer> ProanswerList;

    private Programming programming;

    private List<Programming> programs;
    private Set<Integer> programsId = new HashSet<Integer>();

    private String runtime;

    private int programId;

    private String score;

    private Set<Integer> set = new HashSet<Integer>();

    private TeacherService teacherService;

    private File upload0;

    private String upload0ContentType;

    private String upload0FileName;

    private File upload1;

    private String upload1ContentType;

    private String upload1FileName;

    private File upload2;

    private String upload2ContentType;

    private String upload2FileName;

    private File upload3;

    private String upload3ContentType;

    private String upload3FileName;

    private File upload4;

    private String upload4ContentType;

    private String upload4FileName;

    private File upload5;

    private String upload5ContentType;

    private String upload5FileName;

    private File upload6;

    private String upload6ContentType;

    private String upload6FileName;

    private File upload7;

    private String upload7ContentType;

    private String upload7FileName;

    private File upload8;

    private String upload8ContentType;

    private String upload8FileName;

    private File upload9;

    private String upload9ContentType;

    private String upload9FileName;

    private User user;

    private UserService userService;

    public String addPaper() {
        paper = new Paper();
        paper.setId(1 + teacherService.countPaper());
        return "addpaper";
    }

    public String addProblem() {
        paper = new Paper();
        if (session.get("paperid") == null) {
            id = paperid;

            paper.setId(Integer.parseInt(id));
        } else {

            paper.setId((Integer) session.get("paperid"));
        }

        return "addProblem";
    }

    public String addProgram() {
        return "addProgram";
    }

    public void delPaper() {
        PrintWriter out;
        if (paper == null) {
            paper = new Paper();
        }
        if (teacherService.delPaper(paper.getId())) {
            try {
                out = response.getWriter();
                out.print("1");// 删除成功
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                out = response.getWriter();
                out.print("0");// 删除失败
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public String editPaperPrograms() {
        paper = paperService.findById(Integer.parseInt(id));
        session.put("paperid", paper.getId());
        return "editPaperPrograms";
    }

    public String MyPaperList() {
        currentpage = (currentpage == null ? 1 : currentpage);
        pagesize = (pagesize == null ? 30 : pagesize);
        User temp = (User) session.get("loginUser");
        if (paperPage == null) {
            paperPage = new Pagination(1, 30);
        }
        if (currentpage < 0) {
            currentpage = 1;
        }
        if (currentpage > paperPage.getTotalpagecount() && paperPage.getTotalpagecount() != 0) {
            currentpage = (int) paperPage.getTotalpagecount();
        }
        paperPage.setCurrentpage(currentpage);
        paperPage.setPagesize(pagesize);
        if (paper == null) {
            paper = new Paper();
        }

        paperPage = teacherService.getMyPageList(temp, paperPage, paper);

        return "MyPaperList";

    }

    public String Paperadd() {
        if (paper == null) {
            paper = new Paper();
        }
        paper.setIsvisible("1");
        if (paper.getPaperpw().trim().equals("")) {
            paper.setPaperpw(null);
        }

        User u = (User) session.get("loginUser");
        paper.setUser(u);
        teacherService.addPaper(paper);
        session.put("paperid", paper.getId());
        return "addproblem";
    }

    public void paperclazzadd() {
        response.setContentType("text/html;charset=UTF-8.");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = null;

        paper = teacherService.findByid(paperId);
        clazz = teacherService.findClazzById(Integer.parseInt(id));
        paper.getClasses().add(clazz);
        teacherService.updatePaper(paper);
        try {
            out = response.getWriter();
            out.print("添加成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void delPaperClazz() {
        response.setContentType("text/html;charset=UTF-8.");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = null;

        teacherService.delPaperClazz(Integer.parseInt(id), paperId);
        try {
            out = response.getWriter();
            out.print("删除成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void PaperDel() {
        response.setContentType("text/html;charset=UTF-8.");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = null;
        try {
            int Iid = Integer.parseInt(id);
            paperService.paperDel(Iid);
            out = response.getWriter();
            out.print("删除成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String paperInfo() {

        paper = teacherService.findByid(paperId);
        int s = teacherService.getPaperScore(paperId);
        score = s + "";
        return "paperInfo";
    }

    // 获取试卷表
    public String PaperList() {
        currentpage = (currentpage == null ? 1 : currentpage);
        pagesize = (pagesize == null ? 30 : pagesize);
        User temp = (User) session.get("loginUser");
        if (paperPage == null) {
            paperPage = new Pagination(1, 30);
        }
        if (currentpage < 0) {
            currentpage = 1;
        }
        if (currentpage > paperPage.getTotalpagecount() && paperPage.getTotalpagecount() != 0) {
            currentpage = (int) paperPage.getTotalpagecount();
        }
        paperPage.setCurrentpage(currentpage);
        paperPage.setPagesize(pagesize);
        if (paper == null) {
            paper = new Paper();
        }

        paperPage = teacherService.getPageList(temp, paperPage, paper);
        return "paperList";
    }

    public void paperProAdd() {
        response.setContentType("text/html;charset=UTF-8.");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = null;

        paper = teacherService.findByid(paperId);
        programming = teacherService.findProByid(programId);
        paper.getPaperPrograms().add(programming);
        teacherService.updatePaper(paper);
        try {
            out = response.getWriter();
            out.print("添加成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void paperProDel() {
        response.setContentType("text/html;charset=UTF-8.");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = null;

        id = programId + "";
        teacherService.deletePaperProgram(paperId.toString(), id);
        try {
            out = response.getWriter();
            out.print("删除成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public String Programadd() throws Exception {
        if (programming == null) {
            programming = new Programming();
        }
        User u = (User) session.get("loginUser");
        programming.setUser(u);
        teacherService.addProgram(programming);

        int count = teacherService.findProgram(programming).getId();

        String path = "D:\\dataForPrograms\\" + count;
        File[] files = { getUpload0(), getUpload1(), getUpload2(), getUpload3(), getUpload4(), getUpload5(), getUpload6(), getUpload7(), getUpload8(), getUpload9() };

        File savedir = new File(path);
        if (!savedir.exists()) {
            savedir.mkdirs();
        }
        String filename = null;
        if (files != null) {
            for (int i = 0; i < files.length; i++) {

                if (files[i] != null) {
                    if (i % 2 == 0) {
                        filename = i / 2 + ".in";
                    } else {
                        filename = i / 2 + ".out";
                    }
                    File saveFile = new File(savedir, filename);
                    FileUtils.copyFile(files[i], saveFile);
                }
            }
        }
        return "toprogramList";
    }

    public void ProgramDel() {
        response.setContentType("text/html;charset=UTF-8.");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = null;
        try {
            String path = "D:\\dataForPrograms\\" + id;
            File savedir = new File(path);
            if (savedir.exists()) {
                savedir.delete();
            }
            teacherService.ProgramDel(id);
            out = response.getWriter();
            out.print("删除成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public String programInfo() {
        int tid = Integer.parseInt(id);
        Programming p = new Programming();
        p = teacherService.findProByid(tid);
        session.put("program", p);

        String path = "D:\\dataForPrograms\\" + id;
        File file = new File(path);
        String test[] = file.list();
        for (int i = 0; i < test.length; i++) {

            set.add(Integer.parseInt(test[i].substring(0, 1)));
        }
        session.put("set", set);

        return "programinfo";
    }

    public String ProgramList() {
        if (add == null) {
            if (session.get("add") != null) {
                if (session.get("add").equals("1")) {
                    add = "1";
                } else {
                    add = "0";
                }
            }
        }
        session.put("add", add);

        currentpage = (currentpage == null ? 1 : currentpage);
        int pagesize = 30;
        User temp = (User) session.get("loginUser");

        if (paperPage == null) {
            paperPage = new Pagination(1, 30);
        }
        if (currentpage < 0) {
            currentpage = 1;
        }
        if (currentpage > paperPage.getTotalpagecount() && paperPage.getTotalpagecount() != 0) {
            currentpage = (int) paperPage.getTotalpagecount();
        }

        paperPage.setCurrentpage(currentpage);
        paperPage.setPagesize(pagesize);
        if (programming == null) {
            programming = new Programming();
        }

        if (papername == null) {
            papername = "";
        } else {
            papername = papername.trim();
        }

        paperPage = teacherService.getPrograms(paperPage, programming, papername, temp);

        user = (User) session.get("loginUser");
        Integer intpaperid = (Integer) session.get("paperid");
        if (intpaperid != null) {
            paperid = String.valueOf(intpaperid);
            paper = paperService.findById(intpaperid);
            Set<?> programsSet = paper.getPaperPrograms();
            for (Iterator<?> i = programsSet.iterator(); i.hasNext();) {
                Programming pro = (Programming) i.next();
                programsId.add(pro.getId());
            }
        }

        return "programList";

    }

    public String ProgramBlankList() {
        if (add == null) {
            if (session.get("add") != null) {
                if (session.get("add").equals("1")) {
                    add = "1";
                } else {
                    add = "0";
                }
            }
        }
        session.put("add", add);

        currentpage = (currentpage == null ? 1 : currentpage);
        int pagesize = 30;
        User temp = (User) session.get("loginUser");

        if (paperPage == null) {
            paperPage = new Pagination(1, 30);
        }
        if (currentpage < 0) {
            currentpage = 1;
        }
        if (currentpage > paperPage.getTotalpagecount() && paperPage.getTotalpagecount() != 0) {
            currentpage = (int) paperPage.getTotalpagecount();
        }

        paperPage.setCurrentpage(currentpage);
        paperPage.setPagesize(pagesize);
        if (programming == null) {
            programming = new Programming();
        }

        if (papername == null) {
            papername = "";
        } else {
            papername = papername.trim();
        }

        paperPage = teacherService.getProgramBlanks(paperPage, programming, papername, temp);

        user = (User) session.get("loginUser");
        Integer intpaperid = (Integer) session.get("paperid");
        if (intpaperid != null) {
            paperid = String.valueOf(intpaperid);
            paper = paperService.findById(intpaperid);
            Set<?> programsSet = paper.getPaperPrograms();
            for (Iterator<?> i = programsSet.iterator(); i.hasNext();) {
                Programming pro = (Programming) i.next();
                programsId.add(pro.getId());
            }
        }

        return "programBlankList";

    }

    public String programUpdate() {
        int tid = Integer.parseInt(id);
        Paper p = new Paper();
        p = teacherService.findByid(tid);
        List<Programming> list = new ArrayList<Programming>(p.getPaperPrograms());
        programming = list.get(0);
        session.put("program", list.get(0));
        return "update";
    }

    public String Programupdate() {
        if (programming == null) {
            programming = new Programming();
        }
        programming.setUser((User) session.get("loginUser"));
        teacherService.updateProgram(programming);
        session.put("program", programming);
        return "updateprogram";
    }

    public String StuScore() {

        currentpage = (currentpage == null ? 1 : currentpage);
        pagesize = (pagesize == null ? 30 : pagesize);
        if (paperPage == null) {
            paperPage = new Pagination(1, 30);
        }

        if (currentpage < 0) {
            currentpage = 1;
        }
        if (currentpage > paperPage.getTotalpagecount() && paperPage.getTotalpagecount() != 0) {
            currentpage = (int) paperPage.getTotalpagecount();
        }
        paperPage.setCurrentpage(currentpage);
        paperPage.setPagesize(pagesize);
        if (proanswer == null) {
            proanswer = new Proanswer();
            proanswer.setPaper(new Paper());
            proanswer.setUser(new User());
        }
        paperPage = teacherService.StuScore(proanswer, paperPage);
        return "StuScore";
    }

    public String Updateprogram() {
        programming = teacherService.findProByid(Integer.parseInt(id));
        return "updateprogram";
    }

    public String updatesave() throws Exception {
        if (programming == null) {
            programming = new Programming();
        }
        programming.setUser((User) session.get("loginUser"));
        teacherService.updateProgram(programming);
        String path = "D:\\dataForPrograms\\" + programming.getId();

        /*
         * String[] files_name = getUploadFileName();//上传文件的文件名 for(int i = 0 ; i< files_name.length ; ++i){
         * System.out.println("&*(^*&^*&^^%&*^%&^%&^%&^%&^%*&%&*^%&^%&%&*"+files[i].getName()); }
         */

        /*
         * File savedir = new File(path); if (!savedir.exists()) {//文件夹不存在 savedir.mkdirs(); if(files != null && files.length%2 == 0){//将上传文件按顺序重命名 String filename = null; for (int
         * i = 0; i < files.length; i++) { if (i % 2 == 0) { filename = i / 2 + ".in"; } else { filename = i / 2 + ".out"; } File saveFile = new File(savedir, filename);
         * FileUtils.copyFile(files[i], saveFile); } } }else{//文件夹存在 if(savedir.isDirectory()){ File[] file_sub = savedir.listFiles(); if(file_sub.length==0){//空文件夹 if(files !=
         * null && files.length%2 == 0){//将上传文件按顺序重命名 String filename = null; for (int i = 0; i < files.length; i++) { if (i % 2 == 0) { filename = i / 2 + ".in"; } else { filename
         * = i / 2 + ".out"; } File saveFile = new File(savedir, filename); FileUtils.copyFile(files[i], saveFile); } } }else{//文件夹不为空 if(files != null && files.length%2 ==
         * 0){//将已存在文件覆盖，将不存在文件顺延 for(int i = 0 ; i< files.length ; ++i){//上传的文件 int flag = 0; //不存在 for(int j = 0 ; j< file_sub.length ; j++){//已存在的文件
         * if(files_name[i].equals(file_sub[j].getName())){ flag = 1;//存在 break; } } if(flag==1){//覆盖原文件 File saveFile = new File(savedir, files_name[i]);
         * FileUtils.copyFile(files[i], saveFile); }else{//顺延文件名 File[] file_exist = savedir.listFiles(); String filename = null; if (i % 2 == 0) { filename = file_exist.length / 2
         * + ".in"; } else { filename = file_exist.length / 2 + ".out"; } File saveFile = new File(savedir, filename); FileUtils.copyFile(files[i], saveFile); } } } } } }
         */

        File[] files = { getUpload0(), getUpload1(), getUpload2(), getUpload3(), getUpload4(), getUpload5(), getUpload6(), getUpload7(), getUpload8(), getUpload9() };

        File savedir = new File(path);
        if (!savedir.exists()) {
            savedir.mkdirs();
        }
        String filename = null;
        if (files != null) {
            for (int i = 0; i < files.length; i++) {

                if (files[i] != null) {
                    if (i % 2 == 0) {
                        filename = i / 2 + ".in";
                    } else {
                        filename = i / 2 + ".out";
                    }
                    File saveFile = new File(savedir, filename);
                    FileUtils.copyFile(files[i], saveFile);
                }
            }
        }

        return "toprogramList";
    }

    public void setAdd(String add) {
        this.add = add;
    }

    public void setClazz(Clazz clazz) {
        this.clazz = clazz;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setPaper(Paper paper) {
        this.paper = paper;
    }

    public void setPaperid(String paperid) {
        this.paperid = paperid;
    }

    public void setPaperId(Integer paperId) {
        this.paperId = paperId;
    }

    public void setPapername(String papername) {
        this.papername = papername;
    }

    public void setPaperPage(Pagination paperPage) {
        this.paperPage = paperPage;
    }

    @Resource(name = "PaperService")
    public void setPaperService(PaperService paperService) {
        this.paperService = paperService;
    }

    public void setProanswer(Proanswer proanswer) {
        this.proanswer = proanswer;
    }

    public void setProanswerList(List<Proanswer> proanswerList) {
        ProanswerList = proanswerList;
    }

    public void setProgramming(Programming programming) {
        this.programming = programming;
    }

    public void setPrograms(List<Programming> programs) {
        this.programs = programs;
    }

    public void setProgramsId(Set<Integer> programsId) {
        this.programsId = programsId;
    }

    public void setRuntime(String runtime) {
        this.runtime = runtime;
    }

    public void setScore(String score) {
        this.score = score;
    }

    public void setSet(Set<Integer> set) {
        this.set = set;
    }

    @Resource(name = "TeacherService")
    public void setTeacherService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    public File getUpload0() {
        return upload0;
    }

    public void setUpload0(File upload0) {
        this.upload0 = upload0;
    }

    public String getUpload0ContentType() {
        return upload0ContentType;
    }

    public void setUpload0ContentType(String upload0ContentType) {
        this.upload0ContentType = upload0ContentType;
    }

    public String getUpload0FileName() {
        return upload0FileName;
    }

    public void setUpload0FileName(String upload0FileName) {
        this.upload0FileName = upload0FileName;
    }

    public File getUpload1() {
        return upload1;
    }

    public void setUpload1(File upload1) {
        this.upload1 = upload1;
    }

    public String getUpload1ContentType() {
        return upload1ContentType;
    }

    public void setUpload1ContentType(String upload1ContentType) {
        this.upload1ContentType = upload1ContentType;
    }

    public String getUpload1FileName() {
        return upload1FileName;
    }

    public void setUpload1FileName(String upload1FileName) {
        this.upload1FileName = upload1FileName;
    }

    public File getUpload2() {
        return upload2;
    }

    public void setUpload2(File upload2) {
        this.upload2 = upload2;
    }

    public String getUpload2ContentType() {
        return upload2ContentType;
    }

    public void setUpload2ContentType(String upload2ContentType) {
        this.upload2ContentType = upload2ContentType;
    }

    public String getUpload2FileName() {
        return upload2FileName;
    }

    public void setUpload2FileName(String upload2FileName) {
        this.upload2FileName = upload2FileName;
    }

    public File getUpload3() {
        return upload3;
    }

    public void setUpload3(File upload3) {
        this.upload3 = upload3;
    }

    public String getUpload3ContentType() {
        return upload3ContentType;
    }

    public void setUpload3ContentType(String upload3ContentType) {
        this.upload3ContentType = upload3ContentType;
    }

    public String getUpload3FileName() {
        return upload3FileName;
    }

    public void setUpload3FileName(String upload3FileName) {
        this.upload3FileName = upload3FileName;
    }

    public File getUpload4() {
        return upload4;
    }

    public void setUpload4(File upload4) {
        this.upload4 = upload4;
    }

    public String getUpload4ContentType() {
        return upload4ContentType;
    }

    public void setUpload4ContentType(String upload4ContentType) {
        this.upload4ContentType = upload4ContentType;
    }

    public String getUpload4FileName() {
        return upload4FileName;
    }

    public void setUpload4FileName(String upload4FileName) {
        this.upload4FileName = upload4FileName;
    }

    public File getUpload5() {
        return upload5;
    }

    public void setUpload5(File upload5) {
        this.upload5 = upload5;
    }

    public String getUpload5ContentType() {
        return upload5ContentType;
    }

    public void setUpload5ContentType(String upload5ContentType) {
        this.upload5ContentType = upload5ContentType;
    }

    public String getUpload5FileName() {
        return upload5FileName;
    }

    public void setUpload5FileName(String upload5FileName) {
        this.upload5FileName = upload5FileName;
    }

    public File getUpload6() {
        return upload6;
    }

    public void setUpload6(File upload6) {
        this.upload6 = upload6;
    }

    public String getUpload6ContentType() {
        return upload6ContentType;
    }

    public void setUpload6ContentType(String upload6ContentType) {
        this.upload6ContentType = upload6ContentType;
    }

    public String getUpload6FileName() {
        return upload6FileName;
    }

    public void setUpload6FileName(String upload6FileName) {
        this.upload6FileName = upload6FileName;
    }

    public File getUpload7() {
        return upload7;
    }

    public void setUpload7(File upload7) {
        this.upload7 = upload7;
    }

    public String getUpload7ContentType() {
        return upload7ContentType;
    }

    public void setUpload7ContentType(String upload7ContentType) {
        this.upload7ContentType = upload7ContentType;
    }

    public String getUpload7FileName() {
        return upload7FileName;
    }

    public void setUpload7FileName(String upload7FileName) {
        this.upload7FileName = upload7FileName;
    }

    public File getUpload8() {
        return upload8;
    }

    public void setUpload8(File upload8) {
        this.upload8 = upload8;
    }

    public String getUpload8ContentType() {
        return upload8ContentType;
    }

    public void setUpload8ContentType(String upload8ContentType) {
        this.upload8ContentType = upload8ContentType;
    }

    public String getUpload8FileName() {
        return upload8FileName;
    }

    public void setUpload8FileName(String upload8FileName) {
        this.upload8FileName = upload8FileName;
    }

    public File getUpload9() {
        return upload9;
    }

    public void setUpload9(File upload9) {
        this.upload9 = upload9;
    }

    public String getUpload9ContentType() {
        return upload9ContentType;
    }

    public void setUpload9ContentType(String upload9ContentType) {
        this.upload9ContentType = upload9ContentType;
    }

    public String getUpload9FileName() {
        return upload9FileName;
    }

    public void setUpload9FileName(String upload9FileName) {
        this.upload9FileName = upload9FileName;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Resource(name = "UserService")
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String getAdd() {
        return add;
    }

    public Clazz getClazz() {
        return clazz;
    }

    public String getId() {
        return id;
    }

    public Paper getPaper() {
        return paper;
    }

    public String getPaperid() {
        return paperid;
    }

    public String getPapername() {
        return papername;
    }

    public Pagination getPaperPage() {
        return paperPage;
    }

    public PaperService getPaperService() {
        return paperService;
    }

    public Proanswer getProanswer() {
        return proanswer;
    }

    public List<Proanswer> getProanswerList() {
        return ProanswerList;
    }

    public Programming getProgramming() {
        return programming;
    }

    public List<Programming> getPrograms() {
        return programs;
    }

    public Set<Integer> getProgramsId() {
        return programsId;
    }

    public String getRuntime() {
        return runtime;
    }

    public String getScore() {
        return score;
    }

    public Set<Integer> getSet() {
        return set;
    }

    public TeacherService getTeacherService() {
        return teacherService;
    }

    public User getUser() {
        return user;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setCurrentpage(int currentpage) {
        this.currentpage = currentpage;
    }

    public void setPagesize(Integer pagesize) {
        this.pagesize = pagesize;
    }

}
